package com.situ.web.controller;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.situ.web.entity.City;
import com.situ.web.entity.Province;
import com.situ.web.util.JDBCUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@WebServlet("/china")
public class ChinaServlet extends HttpServlet{

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String method = req.getParameter("method");
		switch (method) {
		case "selectProvince":
			selectProvince(req, resp);
			break;
		case "selectCity":
			selectCity(req, resp);
			break;
		default:
			break;
		}
	}

	private void selectCity(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String provinceId = req.getParameter("provinceId");
		
		Connection connection = null;
		PreparedStatement statement = null;
		ResultSet resultSet = null;
		List<City> list = new ArrayList<City>();
		try {
			connection = JDBCUtil.getConnection();
			String sql = "select id,city from tm_city where province_id=?";
			statement = connection.prepareStatement(sql);
			statement.setInt(1, Integer.parseInt(provinceId));
			System.out.println(statement);
			resultSet = statement.executeQuery();
			while (resultSet.next()) {
				int id = resultSet.getInt("id");
				String city = resultSet.getString("city");
				list.add(new City(id, city));
			}
			for (City city : list) {
				System.out.println(city);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		// 将List<Province> list转换成json格式返回给ajax请求
		// JSONArray JSONObject
		JSONArray jsonArray = JSONArray.fromObject(list);
		resp.setContentType("text/html;charset=utf-8");
		resp.getWriter().write(jsonArray.toString());
	}

	private void selectProvince(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		Connection connection = null;
		PreparedStatement statement = null;
		ResultSet resultSet = null;
		List<Province> list = new ArrayList<Province>();
		try {
			connection = JDBCUtil.getConnection();
			String sql = "select id,province from tm_province";
			statement = connection.prepareStatement(sql);
			System.out.println(statement);
			resultSet = statement.executeQuery();
			while (resultSet.next()) {
				int id = resultSet.getInt("id");
				String province = resultSet.getString("province");
				list.add(new Province(id, province));
			}
			for (Province province : list) {
				System.out.println(province);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		// 将List<Province> list转换成json格式返回给ajax请求
		// JSONArray JSONObject
		JSONArray jsonArray = JSONArray.fromObject(list);
		resp.setContentType("text/html;charset=utf-8");
		resp.getWriter().write(jsonArray.toString());
	}
}
